package controller;



;
import com.google.common.collect.ImmutableMap;
import common.pojo.CommonResult;
import common.utils.JsonUtils;
import handle.WebSocketPushHandler;
import httpclient.HttpClientUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import pojo.UserShopInfo;
import pojo.WxUserInfo;
import service.cfg.ShopCfg;
import service.cfg.WxUserCfg;

import javax.annotation.Resource;

/**
 * Created by asus on 2018/11/19.
 */
/**
 * @ServerEndpoint 注解是一个类层次的注解，它的功能主要是将目前的类定义成一个websocket服务器端,
 * 注解的值将被用于监听用户连接的终端访问URL地址,客户端可以通过这个URL来连接到WebSocket服务器端
 * @author
 */

@Controller
@RequestMapping("/chat")
public class weChatController {

    @Autowired
    WebSocketPushHandler webSocketPushHandler;
    @Autowired
    ShopCfg shopCfg;
    @Autowired
    WxUserCfg wxUserCfg;
    @ResponseBody
    @RequestMapping(value = "/send/msg",method = RequestMethod.POST)
    public CommonResult sendMsg(
            @RequestParam(value = "shopId",required = false) String shopId,
            @RequestParam(value = "customId",required = false) String customId,
            @RequestParam(value = "msg",required = false) String msg
    ){
        if(StringUtils.isBlank(shopId)){
            return CommonResult.returnFailsWrapper("请传入shopId");
        }
        if(StringUtils.isBlank(customId)){
            return CommonResult.returnFailsWrapper("请传入customId");
        }
        if(StringUtils.isBlank(msg)){
            return CommonResult.returnFailsWrapper("请传入msg");
        }
       return webSocketPushHandler.sendMsg(NumberUtils.toLong(shopId),NumberUtils.toLong(customId),msg);
    }

    @ResponseBody
    @RequestMapping(value = "/send/msg/wxId",method = RequestMethod.POST)
    public CommonResult sendMsgWxId(
            @RequestParam(value = "wxId",required = false) String wxId,
            @RequestParam(value = "customId",required = false) String customId,
            @RequestParam(value = "msg",required = false) String msg
    ){
        if(StringUtils.isBlank(wxId)){
            return CommonResult.returnFailsWrapper("请传入wxId");
        }
        if(StringUtils.isBlank(customId)){
            return CommonResult.returnFailsWrapper("请传入customId");
        }
        if(StringUtils.isBlank(msg)){
            return CommonResult.returnFailsWrapper("请传入msg");
        }
        CommonResult commonResult = HttpClientUtils.get(shopCfg.getShopInfo(), ImmutableMap.of("wxId",wxId));
        UserShopInfo shopInfo = JsonUtils.objectToPojo(commonResult.getData(), UserShopInfo.class);
        if(shopInfo==null){
            return CommonResult.returnFailsWrapper("此店主信息不存在");
        }
        return webSocketPushHandler.sendMsg(shopInfo.getId(),NumberUtils.toLong(customId),msg);
    }

}
